﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      Company Search Page
// Created By:     Tingrong Shi (trshi@wicresoft.com)
// Created Date:   2011-2-14
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using ASBCC.Business.CIS;
using System.Web.Services;

namespace BEGDC.CC.Web.CIS2.Domestic.Search
{
    public partial class CompanySearch : PagedPage
    {
        private static string Search = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.edsCompany.Where = Search;
            if (!this.IsPostBack)
            {
                Bind();
                Search = firstbound();
                this.edsCompany.Where = Search;
                this.edsCompany.DataBind();
                GVCompany.Visible = true;
                UserRoles userrole = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
                if (!userrole.IsDomesticAdmin && !userrole.IsOverseaAdmin)
                    this.btnExport.Visible = false;
            }
        }

        protected override EntityDataSource EntitySource
        {
            get { return this.edsCompany; }
        }

        private void Bind()
        {
            //get roles


            //selectProvince
            selectProvince.DataSource = SearchHelper.GetProvinceByRoleIDs((User as CSPrincipal).Role2s, (User as CSPrincipal).Role2String);
            selectProvince.DataValueField = "ProvinceID";
            selectProvince.DataTextField = "Name";
            selectProvince.DataBind();
            selectProvince.Items.Insert(0, new ListItem("--请选择--", "0"));

            //selectCategory
            selectCategory.DataSource = SearchHelper.GetCategoryByRoleIDs((User as CSPrincipal).Role2s, (User as CSPrincipal).Role2String);
            selectCategory.DataValueField = "CateID";
            selectCategory.DataTextField = "Name";
            selectCategory.DataBind();
            selectCategory.Items.Insert(0, new ListItem("--请选择--", "0"));

            //selectCategory
            selectArea.DataSource = SearchHelper.GetAreaByRoleIDs((User as CSPrincipal).Role2s, (User as CSPrincipal).Role2String);
            selectArea.DataValueField = "AreaID";
            selectArea.DataTextField = "Name";
            selectArea.DataBind();
            selectArea.Items.Insert(0, new ListItem("--请选择--", "0"));

            //selectCategory
            selectCountry.DataSource = SearchHelper.GetCountryByRoleIDs((User as CSPrincipal).Role2s, (User as CSPrincipal).Role2String);
            selectCountry.DataValueField = "CountryID";
            selectCountry.DataTextField = "Name";
            selectCountry.DataBind();
            selectCountry.Items.Insert(0, new ListItem("--请选择--", "0"));
            UserRoles userRoles = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
            domestic.Visible = false;
            overseas.Visible = false;
            if (userRoles.IsDomesticSales)
                domestic.Visible = true;
            if (userRoles.IsOverseaSales)
                overseas.Visible = true;



        }

        private string firstbound()
        {
            string roles = (User as CSPrincipal).Role2String;
            UserRoles userRoles = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
            string companyWhere = "!it.Disabled ";
            companyWhere += " and ( (Count( select value pc.Province.ProvinceID from it.PCATs as pc where !pc.Province.Disabled and !pc.Category.Disabled and ";
            companyWhere += string.Format("Count( select value r.RoleID from pc.Roles as r where !r.Disabled and r.RoleID in {{{0}}} ) > 0 ) > 0 ", roles);
            companyWhere += " or Count( select value cu.CountryID from it.Countries as cu where !cu.Disabled and ";
            companyWhere += string.Format("Count( select value r.RoleID from cu.Roles as r where !r.Disabled and r.RoleID in {{{0}}} ) > 0 ) > 0 )", roles);
            if (userRoles.IsDomesticAdmin)
            {
                companyWhere += " or Count( select value pc.Province.ProvinceID from it.PCATs as pc where !pc.Province.Disabled and !pc.Category.Disabled ) > 0";
            }
            if (userRoles.IsOverseaAdmin)
            {
                companyWhere += " or Count( select value cu.CountryID from it.Countries as cu where !cu.Disabled ) > 0";
            }

            companyWhere += " ) ";
            return companyWhere;

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            this.txtCompanyName.Text = PageSafeHelper.SafeText(this.txtCompanyName.Text);
            this.txtEdit.Text = PageSafeHelper.SafeText(this.txtEdit.Text);
            string companyName = this.txtCompanyName.Text;
            string Creator = this.txtEdit.Text;
            string province = PageSafeHelper.SafeText(this.Request.Form["selectProvince"]);
            string category = PageSafeHelper.SafeText(this.Request.Form["selectCategory"]);
            string area = PageSafeHelper.SafeText(this.Request.Form["selectArea"]);
            string country = PageSafeHelper.SafeText(this.Request.Form["selectCountry"]);
            UserRoles userRoles = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
            string companyWhere = firstbound();
            if (!String.IsNullOrEmpty(companyName))
                companyWhere += " and it.Name like \"%" + companyName + "%\"";
            if (!String.IsNullOrEmpty(Creator))
                companyWhere += " and it.CreateBy.NameEN like \"%" + Creator + "%\"";
            if (userRoles.IsDomesticSales)
            {
                if (!String.IsNullOrEmpty(province) && province != "0")
                    companyWhere += " and Count(select value pc.ProvinceID from it.PCATs as pc where pc.ProvinceID = " + province + ")>0";
                if (!String.IsNullOrEmpty(category) && category != "0")
                    companyWhere += " and Count(select value pc.CateID from it.PCATs as pc where pc.CateID = " + category + ")>0";
            }
            if (userRoles.IsOverseaSales)
            {
                if (!String.IsNullOrEmpty(country) && country != "0")
                    companyWhere += " and Count(select value pc.CountryID from it.Countries as pc where pc.CountryID = " + country + ")>0";
                if (!String.IsNullOrEmpty(area) && area != "0")
                    companyWhere += " and Count(select value pc.CountryID from it.Countries as pc where Count( select value ar.AreaID from pc.Areas as ar where ar.AreaID=" + area + ")>0)>0";
            }
            Search = companyWhere;
            edsCompany.Where = Search;
            edsCompany.DataBind();
            this.GVCompany.Visible = true;
            this.GVCompany.PageIndex = 0;
        }
        protected void btnClear_Click(object sender, EventArgs e)
        {
            this.txtCompanyName.Text = "";
            this.txtEdit.Text = "";
            this.selectProvince.SelectedIndex = 0;
            this.selectCategory.SelectedIndex = 0;
            this.selectArea.SelectedIndex = 0;
            this.selectCountry.SelectedIndex = 0;
            this.GVCompany.PageIndex = 0;
            Search = firstbound();
            this.edsCompany.Where = Search;
            this.edsCompany.DataBind();
        }

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
            //base.VerifyRenderingInServerForm(control);
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            //HttpContext.Current.Response.Charset = "UTF-8";
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("MyCompany.xls", Encoding.UTF8).ToString());
            //HttpContext.Current.Response.ContentType = "application/ms-excel";
            //this.GVExport.Visible = true;
            //this.GVExport.AllowPaging = false;
            //this.GVExport.AllowSorting = false;
            //this.GVExport.DataBind();
            //System.IO.StringWriter tw = new System.IO.StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(tw);
            //this.GVExport.RenderControl(hw);
            
            //HttpContext.Current.Response.Write(tw.ToString());
            //HttpContext.Current.Response.End();
            //this.GVExport.Visible = false;

            BEGDC.CC.Web.Control.ExportDataGridToExcel.Export(this.Page, this.GVExport, "MyCompany.xls");
        }

        protected string GetUserType()
        {
            UserRoles userRoles = CompanyInfoHelper.GetUserRightByRoleIDs((this.User as CSPrincipal).Role2s);
            if (userRoles.IsDomesticSales)
                return "dom";
            else
                return "ove";
        }

        protected string GetCil()
        {
            return (User as CSPrincipal).CIL;
        }

        [WebMethod]
        public static string GetCountryByArea(string cil, string areaID)
        {
            return SearchHelper.GetCountryByareaID(cil, areaID);
        }

    }

}
